package com.jcfk.eam.service.equ;

import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.core.utils.BeanUtils;
import com.jcfk.common.security.utils.SecurityUtils;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.eam.dao.equ.EfaLiabilityExtentEquipDao;
import com.jcfk.eam.domain.dto.equ.EfaLiabilityExtentEquipDTO;
import com.jcfk.eam.domain.po.equ.EfaLiabilityExtentEquip;
import com.jcfk.eam.domain.query.equ.EfaLiabilityExtentEquipQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

/**
 * 设备责任范围-设备
 *
 * @author yrl
 * @email
 * @date 2025-04-22
 */
@Service
public class EfaLiabilityExtentEquipService extends BaseService<EfaLiabilityExtentEquipDao, EfaLiabilityExtentEquip> {

    /**
     * EfaLiabilityExtentEquip分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<EfaLiabilityExtentEquipDTO> queryByPage(DataPage<EfaLiabilityExtentEquipDTO> page, EfaLiabilityExtentEquipQuery query) {
        // 分页查询
        query.setEquipCode(StringUtils.isEmpty(query.getEquipCode()) ? query.getEquipCode() : query.getEquipCode() + "%");
        query.setEquipName(StringUtils.isEmpty(query.getEquipName()) ? query.getEquipName() : query.getEquipName() + "%");
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


    /**
     * 判断EfaLiabilityExtentEquip是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EfaLiabilityExtentEquipDTO dto) {
        QueryWrapper<EfaLiabilityExtentEquip> queryWrapper = new QueryWrapper<>();

        return RetResult.ok();
    }

    /**
     * 插入设备责任范围-设备
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EfaLiabilityExtentEquipDTO dto) {

        // 保存
        EfaLiabilityExtentEquip item = new EfaLiabilityExtentEquip();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新设备责任范围-设备
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EfaLiabilityExtentEquipDTO dto) {

        // 保存
        EfaLiabilityExtentEquip item = new EfaLiabilityExtentEquip();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }


    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EfaLiabilityExtentEquip item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setDeleted(0);
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

        item.setLastUpdatedUserName(SecurityUtils.getUserName());

    }

    /**
     * 根据主键获取设备责任范围-设备信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EfaLiabilityExtentEquipDTO get(String id) {
        // 查询数据
        EfaLiabilityExtentEquip item = this.baseMapper.selectById(id);
        EfaLiabilityExtentEquipDTO dto = new EfaLiabilityExtentEquipDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }

    /**
     * 设备责任范围-设备批量保存
     *
     * @param dtos
     * @return StandardCodeDTO
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveBatch(List<EfaLiabilityExtentEquipDTO> dtos, String id) {
        List<EfaLiabilityExtentEquip> insertList = new ArrayList<>();
        List<EfaLiabilityExtentEquip> updateList = new ArrayList<>();
        List<String> notDeleteIds = new ArrayList<>();
        Map<String, EfaLiabilityExtentEquip> existMap = this.list(Wrappers.<EfaLiabilityExtentEquip>lambdaQuery()
                .eq(EfaLiabilityExtentEquip::getLiabilityExtentId, id)
                .in(EfaLiabilityExtentEquip::getEquipId, dtos.stream().map(EfaLiabilityExtentEquipDTO::getEquipId).collect(Collectors.toList())))
            .stream()
            .collect(Collectors.toMap(EfaLiabilityExtentEquip::getEquipId, Function.identity(), (v1, v2) -> v1));

        for (EfaLiabilityExtentEquipDTO dto : dtos) {
            EfaLiabilityExtentEquip item = new EfaLiabilityExtentEquip();

            BeanUtils.copyProperties(dto, item);
            this.setDefaultValue(item);
            // 设置关联主键
            item.setLiabilityExtentId(id);
            EfaLiabilityExtentEquip efaLiabilityExtentEquip = existMap.get(dto.getEquipId());
            if (efaLiabilityExtentEquip != null) {
                notDeleteIds.add(efaLiabilityExtentEquip.getId());
                item.setId(efaLiabilityExtentEquip.getId());
                updateList.add(item);
            } else {
                insertList.add(item);
            }
        }

        // 删除
        QueryWrapper<EfaLiabilityExtentEquip> queryWrapper = new QueryWrapper<>();
        if (notDeleteIds.size() > 0) queryWrapper.notIn("id", notDeleteIds);
        queryWrapper.lambda().eq(EfaLiabilityExtentEquip::getLiabilityExtentId, id);
        this.remove(queryWrapper);

        // 保存新增
        if (insertList.size() > 0) this.saveBatch(insertList);

        // 保存更新
        if (updateList.size() > 0) this.updateBigBatch(updateList);
    }


    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EfaLiabilityExtentEquipDTO>
     */
    public List<EfaLiabilityExtentEquipDTO> getList(EfaLiabilityExtentEquipQuery query) {
        // 查询条件
        QueryWrapper<EfaLiabilityExtentEquip> qw = new QueryWrapper<>();
        qw.lambda().eq(!StrUtil.isEmpty(query.getLiabilityExtentId()), EfaLiabilityExtentEquip::getLiabilityExtentId, query.getLiabilityExtentId());
        qw.lambda().eq(!StrUtil.isEmpty(query.getEquipId()), EfaLiabilityExtentEquip::getEquipId, query.getEquipId());
        qw.lambda().likeRight(!StrUtil.isEmpty(query.getEquipCode()), EfaLiabilityExtentEquip::getEquipCode, query.getEquipCode());
        qw.lambda().likeRight(!StrUtil.isEmpty(query.getEquipName()), EfaLiabilityExtentEquip::getEquipName, query.getEquipName());
        // 查询数据
        List<EfaLiabilityExtentEquip> list = super.list(qw);

        return convertList(list, EfaLiabilityExtentEquipDTO.class);
    }

    /**
     * 根据主键获取设备责任范围-设备信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteItem(String id) {
        this.removeById(id);
    }

    /**
     * 根据设备编号查询设备责任范围-设备信息
     * @param equipCodes 设备编号
     * @param type 责任范围 类型
     * @param neId 不等于的主键id
     * @return List<EfaLiabilityExtentEquipDTO>
     */
    public List<EfaLiabilityExtentEquipDTO> listByCodes(List<String> equipCodes, Integer type, String neId) {
        return this.baseMapper.listByCodes(equipCodes, type, neId);
    }

    public List<EfaLiabilityExtentEquip> listByType(Integer type) {
        return this.baseMapper.listByType(type);
    }
}

